/*
 * poweroff.cpp - power control GUI
 *
 * Copyright (C) 2010-2011 Serge Begger
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "poweroff.h"
#include "ui_poweroff.h"
#include "QDebug"

poweroff::poweroff(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::poweroff)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);    
    consoleKitManager = new QDBusInterface("org.freedesktop.ConsoleKit",
                                     "/org/freedesktop/ConsoleKit/Manager",
                                     "org.freedesktop.ConsoleKit.Manager",
                                     QDBusConnection::systemBus());
}

poweroff::~poweroff()
{
    delete ui;
}

void poweroff::on_cancelButton_pressed()
{
    this->hide();
    emit hideWidget();
}

void poweroff::on_poweroffButton_pressed()
{
    QDBusMessage response = consoleKitManager->call("Stop");
    if(response.type() == QDBusMessage::ErrorMessage){
        myMessage messages(0);
        messages.exec(0,response.errorName()+":"+response.errorMessage());
        return;
    }
    QApplication::quit();
}

void poweroff::on_rebootButton_pressed()
{
    QDBusMessage response = consoleKitManager->call("Restart");
    if(response.type() == QDBusMessage::ErrorMessage){
        myMessage messages(0);
        messages.exec(0,response.errorName()+":"+response.errorMessage());
        return;
    }
    QApplication::quit();
}

void poweroff::on_logoutButton_pressed()
{
    QApplication::quit();
}

void poweroff::on_suspendButton_pressed()
{
    emit disableMedia();
    sleep(0.5);
    QDBusInterface upower("org.freedesktop.UPower",
                                   "/org/freedesktop/UPower",
                                   "org.freedesktop.UPower",
                                   QDBusConnection::systemBus());
    QDBusMessage response = upower.call("Suspend");
    if(response.type() == QDBusMessage::ErrorMessage) {
        myMessage messages(0);
        messages.exec(0,response.errorName()+":"+response.errorMessage());
        return;
    }
    this->hide();
}
